package idv.arthur.work;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ManipulateGridSqGroup extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
       
    public ManipulateGridSqGroup() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	doPost(request,response);
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Widgets wid = new Widgets();
		request.setCharacterEncoding( wid.UIcodeSet );
		response.setContentType("application/text");
		response.setCharacterEncoding( wid.UIcodeSet );
		
		Connection conn = wid.getConn(true);
		PreparedStatement ps = null;
		ResultSet rs = null;
		String errPars = "}";
		String strSQL = "";
		String oper = request.getParameter("oper");			//增刪改查那一種
		final String bssId = "MBMS";
		String subSysId = request.getParameter("subSysId");
		String result = "";
		final String currentUser = request.getParameter("currentUser");
		
		try {
			/** 判別需進行何種操作 **/
			if ( "del".equalsIgnoreCase(oper) ) {
				strSQL = "DELETE FROM squadgroup WHERE sqgruseqid=?";
				ps = conn.prepareStatement(strSQL);
				ps.setInt(1, Integer.parseInt( request.getParameter("sqGruSeqID") ) );
				ps.execute();
				
				wid.keepLogProfile(wid.SQGROUP, wid.DELETE, currentUser, conn, ps);
				result = "刪除完成";
			} else if ( "add".equalsIgnoreCase(oper) ) {
				strSQL = "INSERT INTO squadgroup (sqgruseqid,sqgruseqname,bssid,subsysid,creator,createdDateTime) VALUES (?,?,?,?,?,?)";
				ps = conn.prepareStatement(strSQL);
				ps.setInt(1, wid.getCurrentSN("squadgroup", conn, ps, rs) );
				ps.setString(2, wid.UI2DB( request.getParameter("sqGruSeqName"),conn.getMetaData().getDatabaseProductName() ) );
				ps.setString(3, bssId);
				ps.setString(4, subSysId);
				ps.setString(5, currentUser);
				ps.setTimestamp(6, wid.currentTimeStamp());
				ps.execute();
				
				wid.keepLogProfile(wid.SQGROUP, wid.ADD, currentUser, conn, ps);
				result = "新增完成";
			} else if ( "edit".equalsIgnoreCase(oper) ) {
				strSQL = "UPDATE squadgroup SET sqgruseqname=?,lastUpdator=?,lastUpdatedDateTime=? WHERE sqgruseqid=?";
				if ( wid.isDebug ) {
					System.out.println(strSQL);
				}
				ps = conn.prepareStatement(strSQL);
				ps.setString(1, wid.UI2DB( request.getParameter("sqGruSeqName"),conn.getMetaData().getDatabaseProductName() ));
				ps.setString(2, currentUser);
				ps.setTimestamp(3, wid.currentTimeStamp());
				ps.setInt(4, Integer.parseInt( request.getParameter("sqGruSeqID") ) );
				ps.execute();
				
				wid.keepLogProfile( wid.SQGROUP, wid.UPDATE, currentUser, conn, ps );
				result = "修改完成";
			}			
		}  catch (SQLException e) {
			result = "異動失敗!!\n\n";
			result += wid.errProcess(e);
			wid.kem(conn, ps, e, wid.toEType("SQLException")+errPars, wid.isPrint2Console );
		} catch (UnsupportedEncodingException e) {
			result = "異動失敗!!\n\n";
			result += wid.errProcess(e);
			wid.kem(conn, ps, e, wid.toEType("UnsupportedEncodingException")+errPars, wid.isPrint2Console );
		} catch (Exception e) {
			result = "異動失敗!!\n\n";
			result += wid.errProcess(e);
			wid.kem(conn, ps, e, wid.toEType("Exception")+errPars, wid.isPrint2Console );
		} finally {
			try {
				if ( rs!= null) { rs.close(); }
				if ( ps!= null) { ps.close(); }
				if ( conn!= null) { conn.close(); }
			} catch (SQLException e) {
				System.out.println(wid.errProcess(e));
			}
		}
		
		response.getWriter().write(result);		
	}
}